// packageF/others/customerCenter/component/userDetail/browseRecord/browseRecord.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    keywork: {
      type: null
    },
    memberId: {
      type: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    active: 0,
    getHistoryUrl: "plugin.customer-center.frontend.controllers.customer.get-history",
    getTimeUrl: "plugin.customer-center.frontend.controllers.customer.history",
    getConfig: { client: "work", type: 17 }, //企业微信公共参数
    times: [],
    history: [],
    timeIndex: '',
  },
  lifetimes: {
    attached() {
      let language = wx.getStorageSync("langIndex");
      this.setData({
        language: language.en,
      });
      this.getTimeList();
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getTimeList() {
      let json = {};
      if (this.data.keywork == 'customer') {
        this.data.getTimeUrl = 'plugin.customer-center.frontend.controllers.customer.history';
        json.member_id = this.data.memberId;
      }
      let urlStr = app.getNetAddresss(this.data.getTimeUrl);
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            if (res.data.time_data.length > 0) {
              res.data.time_data.forEach((item) => {
                this.data.times.push({
                  time: item,
                  list: [],
                  load: { page: 1, finished: false },
                  isShow: false,
                });
              });
              this.setData({
                times: this.data.times
              })
            } else {
              // this.finished = true;
            }
          } else {

            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    },
    closeHistorys(e){
      let index = e.currentTarget.dataset.index;
      let historyData = this.data.times[index];
      historyData.isShow = !historyData.isShow;
      historyData.load.finished = false;
      this.setData({
        timeIndex: index,
        ['times[' + index + ']']: historyData
      })
    },
    showHistorys(e) {
      console.log(e)
      let index = e.currentTarget.dataset.index;
      let historyData = this.data.times[index];
      historyData.isShow = !historyData.isShow;
      this.setData({
        timeIndex: index,
        ['times[' + index + ']']: historyData
      })
      console.log(historyData)
      if (!historyData.isShow) {
        return;
      }
      this.getHistory();
    },
    copyMethod(e){
      let url = e.currentTarget.dataset.url;
      wx.redirectTo({
        url: '/' + url,
        fail: (err) => {
          wx.setClipboardData({
            data: url,
            success: () => {
              wx.hideToast()
              wx.showModal({
                content: '复制成功',
                showCancel: false
              })
            }
          })
        }
      })
    },
    getHistory(e) {
      if(e && this.data.times[e.currentTarget.dataset.ind]){
        if(this.data.times[e.currentTarget.dataset.ind].isShow){
          this.setData({
            timeIndex:e.currentTarget.dataset.ind
          })
        }
      }
      let historyData = this.data.times[this.data.timeIndex];
      if (historyData.load.finished) {
        app.tips('没用更多了');
        return;
      }
      let json = {
        page: historyData.load.page,
        get_time: historyData.time,
      };
      let obj = {}
      if (this.data.keywork == 'customer') {
        this.getHistoryUrl = 'plugin.customer-center.frontend.controllers.customer.get-history';
        json.member_id = this.data.memberId;
        obj = { ...json };
      }
      let urlStr = app.getNetAddresss(this.data.getHistoryUrl);
      app._postNetWork({
        url: urlStr,
        data: obj,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            if (historyData.load.page >= res.data.last_page || res.data.data.length < res.data.per_page) {
              historyData.load.finished = true;
            }
            historyData.list = historyData.load.page > 1 ? historyData.list.concat(res.data.data) : res.data.data;
            historyData.load.page = ++historyData.load.page;
            this.setData({
              ['times[' + this.data.timeIndex + ']']: historyData
            })
            console.log(this.data.times)
          } else {

            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
})
